/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, template, Collection, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },
        fetch: function (_args) {
            var _this = this;
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            args.type = type;

            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'agency_monthly_reports',
                condition: args,
                model: {
                    tag_name: 'tr',
                    event: {},
                    change: function (e) {
                        this.data[$(e.target).attr('name')] = $(e.target).val();
                    }
                },
                callback: function () {
                    loaded();
                    if (!_this.month_list) {
                        _this.month_list = _this.collection.result.month_list;
                        _this.set_year();
                    }
                    $('.pagination').prepend('<li><a href="javascript:">结果合计：￥' + _this.collection.result._amount + '</a></li>');
                    $('.pagination').prepend('<li><a href="javascript:">总计：￥' + _this.collection.result._page_amount + '</a></li>');
                }
            });
            this.collection.fetch();
        },
        set_year: function () {
            var _this = this;
            var list = _this.month_list ? _this.month_list : [];
            for (var n in list) {
                if (!isNaN(n)) {
                    $('select[name="year"]').append('<option value="' + n + '">' + n + '</option>');
                }
            }
        },
        event: function () {
            $('#start_time').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

            $('#end_time').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

            $('#all').on('click', function(e) {
                var r = $(this).prop('checked');
                $('td :checkbox').prop('checked', r);
            });

            var _this = this;

            var reloadData = function() {
                var keywords = $('#keywords').val();
                var timeType = $('#time_type').val();
                var startTime = $('#start_time').val();
                var endTime = $('#end_time').val();
                var status = $('.btn-group .active').attr('data-item');

                _this.collection.condition.status = status;
                _this.collection.condition.time_type = timeType;
                _this.collection.condition.keywords = keywords;
                _this.collection.condition.start_time = startTime;
                _this.collection.condition.end_time = endTime;
                _this.collection.reload();
            }

            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();

                reloadData();
            });

            $('.btn-group button').on('click', function(e) {
                $('.btn-group button').removeClass('active');
                $(this).addClass('active');

                if ($(this).attr('data-item') == 2) {
                    $('#withdraw').show();
                } else {
                    $('#withdraw').hide();
                }

                reloadData();

            });

            $('#withdraw').on('click', function() {
                var box = $('tr :checkbox:checked');
                var id = [];
                box.each(function(index, value) {
                    var v = $(value).val();
                    if (v > 0) {
                        id.push(v);
                    }
                });

                if (id.length == 0) {
                    Alert({msg:"请至少选择一个有效的订单", type:"error"});
                    return false;
                }

                $.post(_global.url.api+'withdraw', {id: JSON.stringify(id)}, function(data) {
                    if (data.code == 333) {
                        var d = dialog({
                            title: '提示',
                            content: '补充提现信息?',
                            okValue: '确定',
                            ok: function () {
                                window.location.href = '/agency/withdraw_info';
                                return false;
                            },
                            cancelValue: '取消',
                            cancel: function () {}
                        });
                        d.show();
                        return false;
                    }
                    var t = {msg: data.msg};
                    if (data.code != 0) {
                        t.type = "error";
                    }
                    Alert(t);
                });
            });

        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});